! test IO of file
module aerof
    implicit none
    
    type fandm
        real :: t, fx, fy, fz, mx, my, mz
    end type fandm 

end module aerof

program testio
    use aerof
    implicit none 

    character(len=2) :: jug
    character(len=4) :: vel
    character(len=12) :: af = "rotor_force_"
    character(len=35) :: filename
    character(len=60) :: newfile
    integer :: val, istep, i, inum, itmp, irot
    real,allocatable :: fxx(:),fyy(:),fzz(:)
    real,allocatable :: mxx(:),myy(:),mzz(:)
    type(fandm), allocatable :: arr(:)
    real :: rsp 

    val = 0
    istep = 76
    ! inum = 73

    print *,"Please input the dat file name:"
    print *,"=== 0 mannul input file name--->"
    print *,"=== 1 is equal to 'upper rotor'"
    print *,"=== 2 is equal to 'lower rotor'"
    read(*,*) irot 
    if(irot == 1) then 
        filename = "rcr_upper_rotor.dat"
    elseif(irot == 2) then 
        filename = "rcr_lower_rotor.dat"
    elseif(irot == 0) then 
        print *,"Please input the dat file name Mannually"
        read *, filename
    else 
        print *,"Error: Invaild Input! Must be 1 or 2."
    end if 

    open(unit=10, file=filename, status='old')
    100 format(A2)
    200 format(1XE23.15, 1XE23.15, 1XE23.15, 1XE23.15, 1XE23.15, 1XE23.15, 1XE23.15)
    do i=1,istep
        read(unit=10, fmt=100) jug ! Fortran 默认读一行换一行，那么我在读到正确的一行的时候，该怎么不跳行呢？
                                    ! 有一个折中的办法，就是多写一行，这一行最后就丢弃了（也可以是个空行）
                                    ! 暂时先这么处理，先解决有无的问题再说
        ! print *,"The header of line is: ", jug
        if(jug == " #") then
            ! print *, "This line is unvaild."
        else 
            ! print *, "This line num is:", i
            itmp = i
            exit
        end if 
    end do 

    inum = istep-itmp + 1
    allocate(arr(inum)) ! 
    allocate(fxx(inum),fyy(inum),fzz(inum),mxx(inum),myy(inum),mzz(inum))

    do i=itmp,istep
        ! print *, "This line num is:", i
        read(unit=10, fmt=200) arr(i-itmp+1)%t, arr(i-itmp+1)%fx, arr(i-itmp+1)%fy, arr(i-itmp+1)%fz, &
                            arr(i-itmp+1)%mx, arr(i-itmp+1)%my, arr(i-itmp+1)%mz 
    end do 

    close(unit=10)

    do i=1,inum
        fxx(i) = arr(i)%fx
        fyy(i) = arr(i)%fy
        fzz(i) = arr(i)%fz
        mxx(i) = arr(i)%mx
        myy(i) = arr(i)%my
        mzz(i) = arr(i)%mz
    end do 

    print *, "Please Input the flight velocity:"
    read(*,"(A4)") vel

    if(irot == 1) then 
        newfile = af//vel//"_upper_rotor.dat"
    elseif(irot == 2) then 
        newfile = af//vel//"_lower_rotor.dat"
    else 
        newfile = af//vel//filename
    end if 

    print *, "Please Input the Rotation Speed(rad/s):"
    read(*,*) rsp

    write(*,"(A40)") "=== The Average Force of Rotor ===" 
    write(*,"(A20,F16.3,A3)") "Average of Fx is:", sum(fxx)/size(fxx)/9.8, "kg", &
    "Average of Mx is:", sum(mxx)/size(mxx), "N*M"
    write(*,"(A20,F16.3,A3)") "Average of Fy is:", sum(fyy)/size(fyy)/9.8, "kg", &
    "Average of My is:", sum(myy)/size(myy), "N*M"
    write(*,"(A20,F16.3,A3)") "Average of Fz is:", sum(fzz)/size(fzz)/9.8, "kg", &
                        "Average of Mz is:", sum(mzz)/size(mzz)*rsp/1000, "kW"

    open(unit=20, file=newfile, status='new')

    write(20,"(A40)") "=== The Average Force of Rotor ===" 
    write(20,"(A20,F16.3,A3)") "Average of Fx is:", sum(fxx)/size(fxx)/9.8, "kg", &
    "Average of Mx is:", sum(mxx)/size(mxx), "N*M"
    write(20,"(A20,F16.3,A3)") "Average of Fy is:", sum(fyy)/size(fyy)/9.8, "kg", &
    "Average of My is:", sum(myy)/size(myy), "N*M"
    write(20,"(A20,F16.3,A3)") "Average of Fz is:", sum(fzz)/size(fzz)/9.8, "kg", &
                        "Average of Mz is:", sum(mzz)/size(mzz)*rsp/1000, "kW"

    close(unit=20)
    deallocate(fxx, fyy, fzz, mxx, myy, mzz, arr)
    
end program testio